﻿using ACC.Business;
using ACC.Business.Stn;
using ACC.Data;
using ACC.Data.ACCLog;
using ACC.Data.Comm;
using ACC.Data.Entities.Common;
using ACC.Data.Entities.Mes;
using ACC.Data.Line;
using ACC.Device;
using ACC.LineDevice.KukaPack;
using System;
using System.Collections.Generic;
using System.Threading;

namespace Script
{

    public class L1P06052 : StnProcessKukaPack
    {
        public L1P06052(StnRunningData data)
            : base(data)
        {
            materialTypeCount = 1;
        }
        protected override ACCDevice CreateDevice()
        {
            ACCDevice dev = base.CreateDevice();
            if (kukaSiemensDev != null)
            {
                kukaSiemensDev.DB190 = 800;
                kukaSiemensDev.DB190_Offset = 19000;
                kukaSiemensDev.DB192 = 80;
                kukaSiemensDev.DB192_PLC_Offset = 19066;

                kukaSiemensDev.DB193 = 800;
                kukaSiemensDev.DB193_PLC_Offset = 19034;
                kukaSiemensDev.DB193_ACC_Offset = 19068;
                kukaSiemensDev.DB194 = 800;
                kukaSiemensDev.DB194_PLC_Offset = 19034;
                kukaSiemensDev.DB194_PLC_Data_Offset = 19078;

            }

            return dev;

        }

        public override ACCCode SendUnloadResult(ACCDevice dev, ACCDevice.UnloadResult result)
        {
            string msg = "";
            ACCCode code = base.SendUnloadResult(dev, result);
            if (code == ACCCode.ACCOK && result.ErrorCode == ACCCode.ACCOK)
            {
                try
                {
                    var param = new Dictionary<string, string>();
                    //param.Add("line", _lineData.Line);
                    //param.Add("op", _data.StnCfg.OPCfg.OP);
                    //param.Add("stn", _data.StnCfg.Name);
                    //param.Add("unitsn", _data.LastUnloadUnit.UnitSN);

                    //PostWebApiMessage("UpdateCurrentUnitSNStatus", "", param);


                    // 键合机工位过程数据采集
                    param.Clear();
                    param.Add("line", _lineData.Line);
                    param.Add("stn", _data.StnCfg.Name);
                    param.Add("barcode", GetGBSN(_data.LastUnloadUnit.UnitSN, _lineData.Line, "GBSN"));
                    param.Add("unitsn", _data.LastUnloadUnit.UnitSN);

                    new Thread(new ThreadStart(delegate()
                    {
                        PostWebApiMessage("ImportBonderData", "", param);

                    })).Start();

                }
                catch (Exception ex)
                {
                    logger.Error(ex.StackTrace);
                }
                StnEventLog.Log(_data, StnLog.LogType.Error,
                        string.Format("出站报工{0} UnitSN={1}, ErrorMessage={2}", "失败", _data.LastUnloadUnit.UnitSN, msg));

            }

            return code;
        }

    }
}